﻿using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
#if PC
namespace OverlakeApp.ViewModels
{
    ///
    /// This inspector adds a user agent to the outgoing web request. It is based on code at:
    /// http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/
    ///
    class HttpUserAgentMessageInspector : IClientMessageInspector
    {
        private const string UserAgentHTTPHeader = "user-agent";
        private readonly string _userAgent;

        public HttpUserAgentMessageInspector(string userAgent)
        {
            _userAgent = userAgent;
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            HttpRequestMessageProperty httpRequestMessage;
            Object httpRequestMessageObject;
            if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
            {
                httpRequestMessage = (HttpRequestMessageProperty)httpRequestMessageObject;
                if (String.IsNullOrEmpty(httpRequestMessage.Headers[UserAgentHTTPHeader]))
                {
                    httpRequestMessage.Headers[UserAgentHTTPHeader] = _userAgent;
                }

            }
            else
            {
                httpRequestMessage = new HttpRequestMessageProperty();
                httpRequestMessage.Headers[UserAgentHTTPHeader] = _userAgent;
                //httpRequestMessage.Headers.Add(UserAgentHTTPHeader, _userAgent);
                request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
            }
            return null;
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            return;
        }
    }

    ///
    /// This behavior adds a user agent to the outgoing web request. It is based on code at:
    /// http://anoriginalidea.wordpress.com/2010/08/19/adding-the-user-agent-when-calling-a-web-service-with-wcf/
    ///
    class HttpUserAgentEndpointBehavior : IEndpointBehavior
    {
        public static string UserAgent;

        public HttpUserAgentEndpointBehavior()
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            return;
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            return;
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            return;
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            var inspector = new HttpUserAgentMessageInspector(UserAgent);
            clientRuntime.MessageInspectors.Add(inspector);
        }
    }
}
#endif